package top.wilbert.collegelink.classification.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.wilbert.collegelink.classification.entity.OccupationEntity;
import top.wilbert.collegelink.classification.entity.RegionEntity;
import top.wilbert.collegelink.classification.service.OccupationService;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.R;


/**
 * @author Augus
 * @email 2531496343@qq.com
 * @date 2023-02-21 09:32:39
 */
@RestController
@RequestMapping("classification/occupation")
public class OccupationController {
    @Autowired
    private OccupationService occupationService;


    /**
     * 增加职业分类
     * occupationId 在该分类下顺序插入
     *
     * @param pid
     * @param name
     * @return
     */
    @RequestMapping("/add")
    public R add(@RequestParam Integer pid, @RequestParam String name) {
        List<OccupationEntity> listByPid = occupationService.getListByPid(pid);
        OccupationEntity finalOccupation = listByPid.get(listByPid.size() - 1);
        OccupationEntity occupationEntity = new OccupationEntity();
        occupationEntity.setOccupationId(finalOccupation.getOccupationId() + 1);
        occupationEntity.setPid(pid);
        occupationEntity.setName(name);
        occupationService.save(occupationEntity);

        return R.ok();
    }

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/listTree")
    public R listTree() {
        List<OccupationEntity> occupationEntities = occupationService.listWithTree();
        return R.ok().setData(occupationEntities);
    }

    /**
     * 将数据库表的name_id和pid字段转换为指定格式
     * 每一层类型数量不能超过99，超过需修改每一层编号为三位数
     * 数据表需要有自增加的主键id，表更新后删除id字段并更改成需要的主键
     * 表更新后，此方法应舍弃，防止二次调用，破坏表数据
     */
//    @RequestMapping("/updateTreeId")
//    public void updateTreeId(){
//        List<OccupationEntity> occupationLevel1Entities = occupationService.getListByPid("-1");
//        int idLevel1 = 1;
//        int idLevel2 = 1;
//        int idLevel3 = 1;
//        String idString1 = "";
//        String idString2 = "";
//        String idString3 = "";
//        for(int i = 0; i < occupationLevel1Entities.size(); i++){
//            String id1 = occupationLevel1Entities.get(i).getOccupationId();
//            idString1 = "" + idLevel1;
//            occupationLevel1Entities.get(i).setOccupationId(idString1);
//            occupationService.updateById(occupationLevel1Entities.get(i));
//            idLevel1++;
//            List<OccupationEntity> occupationLevel2Entities = occupationService.getListByPid(id1);
//            idLevel2 = 1;
//            for(int j = 0; j < occupationLevel2Entities.size(); j++){
//                occupationLevel2Entities.get(j).setPid(idString1);
//                String id2 = occupationLevel2Entities.get(j).getOccupationId();
//                if(idLevel2 < 10){
//                    idString2 = "0" + idLevel2;
//                    idString2 = occupationLevel2Entities.get(j).getPid() + idString2;
//                }else{
//                    idString2 = occupationLevel2Entities.get(j).getPid() + idLevel2;
//                }
//                occupationLevel2Entities.get(j).setOccupationId(idString2);
//                occupationService.updateById(occupationLevel2Entities.get(j));
//                idLevel2++;
//                List<OccupationEntity> occupationLevel3Entities = occupationService.getListByPid(id2);
//                idLevel3 = 1;
//                for(int k = 0; k < occupationLevel3Entities.size(); k++){
//                    occupationLevel3Entities.get(k).setPid(idString2);
//                    if(idLevel3 < 10){
//                        idString3 = "0" + idLevel3;
//                        idString3 = occupationLevel3Entities.get(k).getPid() + idString3;
//                    }else{
//                        idString3 = occupationLevel3Entities.get(k).getPid() + idLevel3;
//                    }
//                    occupationLevel3Entities.get(k).setOccupationId(idString3);
//                    occupationService.updateById(occupationLevel3Entities.get(k));
//                    idLevel3++;
//                }
//            }
//        }
//    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("subject:occupation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = occupationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{occupationId}")
//    @RequiresPermissions("subject:occupation:info")
    public R info(@PathVariable("occupationId") Long occupationId) {
        OccupationEntity occupation = occupationService.getById(occupationId);

        return R.ok().put("occupation", occupation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("subject:occupation:save")
    public R save(@RequestBody OccupationEntity occupation) {
        occupationService.save(occupation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("subject:occupation:update")
    public R update(@RequestBody OccupationEntity occupation) {
        occupationService.updateById(occupation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("subject:occupation:delete")
    public R delete(@RequestBody Long[] occupationIds) {
        occupationService.removeByIds(Arrays.asList(occupationIds));

        return R.ok();
    }

}
